using System.Collections.Generic;

using FreestyleCoding.CardLib;

namespace CardRoulette {
	public static class Statistics {
		internal static int m_CardCount;
		internal static int m_JokerCount;
		internal static Dictionary<IPlayingCard, int> m_CardDictionary;

		static Statistics() {
			InitializeFields();
		}

		private static void InitializeFields() {
			m_CardCount = 0;
			m_JokerCount = 0;

			if( m_CardDictionary == null ) {
				m_CardDictionary = new Dictionary<IPlayingCard, int>( 54 );
			} else {
				m_CardDictionary.Clear(); 
			}

			foreach( IPlayingCard _Card in PokerDeck.New( true ) ) {
				m_CardDictionary.Add( _Card, 0 );
			}
		}

		private static void CalculateStatisticsForSingleCard( IPlayingCard p_Card ) {
			++m_CardDictionary[p_Card];

			if( p_Card.Rank == Ranks.Joker )
				++m_JokerCount;
			else
				++m_CardCount;
		}

		public static void Calculate( IDeck<IPlayingCard> p_Deck ) {
			InitializeFields();
			Update( p_Deck );
		}

		public static void Update( IDeck<IPlayingCard> p_Deck ) {
			foreach( IPlayingCard _Card in p_Deck ) {
				CalculateStatisticsForSingleCard( _Card );
			}
		}

		public static Payout Update( IPlayingCard p_Card ) {
			//TODO: For Milestone 3, populate Payout object with odds
			// Must be done before the stats update
			Payout _Payout = new Payout();

			--m_CardDictionary[p_Card];
			if( p_Card.Rank == Ranks.Joker ) {
				--m_JokerCount;
			} else {
				--m_CardCount;
			}

			return _Payout;
		}
	}
}
